orderController.$inject = ['$scope', '$stateParams', 'orderService'];

function orderController($scope, $stateParams, orderService) {
    
    /**
     * get order
     */
    orderService.getOrderById($stateParams.orderId).then(
        function(response) {
            if (response.data.result) {
                $scope.order = response.data.data;

                /**
                 * get comment in order
                 */
                orderService.getCommentByOrderId($stateParams.orderId).then(
                    function(response) {
                        // console.log(response.data);
                        if (response.data.data) {
                            $scope.order.comment = response.data.data;
                        }
                    },
                    function(err) {
                        console.log(err.data);
                    }
                );
            }
        },
        function(err) {
            console.log(err.data);
        }
    );


    /**
     * Customer confirm order
     */
    $scope.confirmOrder = function() {
        orderService.confirmOrder($stateParams.orderId).then(
            function(response) {
                if (response.data.result) {
                    window.location.reload();
                }
            },
            function(err) {
                console.log(err.data);
            }
        );
    }


    /**
     * Customer send comment
     */
    $scope.sendComment = function() {
        orderService.sendComment({
            star: 3.0,
            content: $scope.commentText,
            createTime: new Date(),
            merchant: $scope.order.merchant._id,
            order: $stateParams.orderId
        }).then(
            function(response) {
                if (response.data.result) {
                    // console.log('comment success!');
                    // hideModal('comment');
                    window.location.reload();
                }
            },
            function(err) {
                console.log(err.data);
            }
        );
    }


    /**
     * Modal operations
     */
    $scope.showModal = function(modalName) {
        $('#o2oc-order-panel #' + modalName + '-modal').modal('show');
    }

    function hideModal(modalName) {
        $('#o2oc-order-panel #' + modalName + '-modal').modal('hide');
    }
}

module.exports = orderController;